#define GLUT_DISABLE_ATEXIT_HACK
#include "Camera.h"
#include "Input/Clavier.h"
#include "Input/Souris.h"


Camera::Camera(Vector3D & position):ObjetInteractive(position),_speed(0.8),_sensivity(0.8),_theta(-180),_phi(-7)
{
    VectorsFromAngles();
}
Camera::Camera():_speed(0.5),_sensivity(0.8),_theta(-180),_phi(-30)
{
	_position = Vector3D(20,18,1) ;
	VectorsFromAngles();
}

Camera::~Camera()
{
}

void Camera::afficher()
{
	
	gluLookAt(_position.X,_position.Y,_position.Z,
              _target.X,_target.Y,_target.Z,
              0,1,0);
}
void Camera::clavier()
{
	
}
void Camera::toucheSpecial()
{
	switch (Clavier::Instance().getSpecial()) {
	      case GLUT_KEY_UP:
	    	  _position += _forward * _speed;
	         break;
	      case GLUT_KEY_DOWN:
	    	  _position -= _forward * _speed;
	         break;
	      case GLUT_KEY_RIGHT:
	    	 _position += _left * _speed;
	         break;
	      case GLUT_KEY_LEFT:
	    	 _position -= _left *_speed;
	         break;
	   }
	 _target = _position + _forward;
	//cout<<" X :"<<_position.X<<" Y :"<<_position.Y<<" Z :"<<_position.Z<<endl;
}
void Camera::souris()
{
}
void Camera::mouvementSouris()
{
}
void Camera::mouvementClickSouris()
{
	switch(Souris::Instance().getBouton())
	{
	case GLUT_RIGHT_BUTTON :
		_theta += Souris::Instance().getDiffX()*_sensivity;
		_phi -= Souris::Instance().getDiffY()*_sensivity;
		VectorsFromAngles();
		break;
	case GLUT_MIDDLE_BUTTON:
		_position += Vector3D(0,Souris::Instance().getDiffY()*_sensivity,0);
		_target = _position + _forward;
		break;
	}
	cout<<" X :"<<_position.X<<" Y :"<<_position.Y<<" Z :"<<_position.Z<<endl;
	//cout<<" Theta : "<<_theta<<" Phi : "<<_phi<<endl;
}
void Camera::setSpeed(double speed)
{
    _speed = speed;
}

void Camera::setSensivity(double sensivity)
{
    _sensivity = sensivity;
}

void Camera::setPosition(const Vector3D & position)
{
    _position = position;
    _target = _position + _forward;

}
void Camera::VectorsFromAngles()
{
   static const Vector3D up(0,1,0);
    if (_phi > 89)
        _phi = 89;
    else if (_phi < -89)
        _phi = -89;
    double r_temp = cos(_phi*PI/180);
    _forward.Y = sin(_phi*PI/180);
    _forward.X = r_temp*cos(_theta*PI/180);
    _forward.Z = r_temp*sin(_theta*PI/180);

    _left = up.crossProduct(_forward);
    _left.normalize();
    _target = _position + _forward;
}
